<script setup lang="ts">
import { ElMessage, ElMessageBox } from 'element-plus'
import type { ChanWuRow, JianCeRow } from '../create'
import { useEntPageData } from '../createHelper'
import chanWuEdit from './chan.wu.edit.vue'
import jianceEdit from './jiance.edit.vue'
import { ModelType } from '@/types/common'
import type { doLoading } from '@/types/page'
import useEntStore from '@/store/modules/ent'
import entApi from '@/api/modules/ent'

const entStore = useEntStore()
const { refreshDevices } = entStore
const { isReadOnly } = useEntPageData()

const editRef = ref()

// 产污
function toCreate() {
  editRef.value.open({ type: ModelType.add })
}

function toEdit(row: ChanWuRow) {
  editRef.value.open({ type: ModelType.edit, data: row })
}

async function submitEdit(formData: ChanWuRow, doLoading: doLoading) {
  try {
    doLoading(true)
    const baseInfoObjID = entStore.currentEntData.baseInfo?.ObjID
    if (!baseInfoObjID) {
      return
    }
    const isCreate = !formData.ObjID
    isCreate
      ? await entApi.ProduceAdd({
        enterpriseObjID: baseInfoObjID,
        produce: formData,
      })
      : await entApi.ProduceUpdate({
        produce: formData,
        produceObjID: formData.ObjID,
      })
    refreshDevices(baseInfoObjID)
    editRef.value.close()
    ElMessage.success({
      message: isCreate ? '新增成功' : '编辑成功',
      type: 'success',
    })
  }
  catch (e) {
    doLoading(false)
  }
}

async function delEnt(row: ChanWuRow) {
  await ElMessageBox.confirm(`确定删除产污设施 "${row.Name}" 吗？`, '提示', {
    type: 'warning',
  })
  await entApi.ProduceDelete({ produceObjID: row.ObjID })
  ElMessage.success({
    message: '删除成功',
    type: 'success',
  })
  refreshDevices(entStore.currentEntData.baseInfo?.ObjID)
}

// 监测点
const jianceEditRef = ref()
function toEditJianCe(row: JianCeRow) {
  jianceEditRef.value.open({ type: ModelType.edit, data: row })
}

function toAddJianCe(row: ChanWuRow) {
  jianceEditRef.value.open({ type: ModelType.add, data: { ParentId: row.ObjID } })
}

async function submitJianCe(formData: JianCeRow, doLoading: doLoading) {
  try {
    doLoading(true)
    const isCreate = !formData.ObjID
    isCreate
      ? await entApi.ProduceAddElectricDevice({
        produceObjID: formData.ParentId,
        electricDevice: formData,
      })
      : await entApi.ProduceUpdateElectricDevice({
        electricDevice: formData,
        electricDeviceObjID: formData.ObjID,
      })
    refreshDevices(entStore.currentEntData.baseInfo?.ObjID)
    jianceEditRef.value.close()
    ElMessage.success({
      message: isCreate ? '新增成功' : '编辑成功',
      type: 'success',
    })
  }
  catch (e) {
    doLoading(false)
  }
}

async function delJianCe(row: JianCeRow, parent: ChanWuRow) {
  await ElMessageBox.confirm(`确定删除监测点"${row.Name}" 吗？`, '提示', {
    type: 'warning',
  })
  await entApi.ProduceDeleteElectricDevice({ electricDeviceObjID: row.ObjID, produceObjID: parent.ObjID })
  ElMessage.success({
    message: '删除成功',
    type: 'success',
  })
  refreshDevices(entStore.currentEntData.baseInfo?.ObjID)
}
</script>

s
<template>
  <div>
    <div v-if="!isReadOnly" class="mb-4 flex flex-justify-end">
      <el-button type="success" @click="toCreate">
        新增产污设施
      </el-button>
    </div>
    <div v-auto-height="50">
      <vxe-table
        auto-resize border
        height="100%"
        :loading="entStore.currentEntLoading"
        :data="entStore.Produces"
        :row-config="{ isCurrent: true, keyField: 'ObjID' }"
        :expand-config="{
          accordion: true,
          reserve: true,
        }"
      >
        <vxe-column type="expand" width="80">
          <template #content="{ row }">
            <div class="p-4">
              <vxe-table
                border
                header-row-class-name="bg-#bae0ff"
                :data="row.Children"
                :row-config="{ isCurrent: true }"
              >
                <vxe-column field="Name" title="产污监测点" min-width="100" />
                <vxe-column field="AssetCode" title="资产号" min-width="100" />
                <vxe-column field="MN" title="MN号" min-width="100" />
                <vxe-column field="PointCode" title="采集因子" min-width="100" />
                <vxe-column field="Location" title="安装位置" min-width="100" />
                <VxeColumn v-if="!isReadOnly" field="operate" title="操作" width="200">
                  <template #default="{ row: ChildRow }">
                    <ElButton link type="success" @click="toEditJianCe(ChildRow)">
                      编辑
                    </ElButton>
                    <ElButton link type="danger" @click="delJianCe(ChildRow, row)">
                      删除
                    </ElButton>
                  </template>
                </VxeColumn>
              </vxe-table>
            </div>
          </template>
        </vxe-column>
        <VxeColumn field="Name" title="产污设施名称" min-width="100" />
        <VxeColumn field="LimitLower" title="阈值（kW）" min-width="100" />
        <VxeColumn field="Location" title="安装位置" min-width="100" />
        <VxeColumn field="PointCode" title="监测点数" width="150">
          <template #default="{ row }">
            {{ row.ElectricDevices.length }}
          </template>
        </VxeColumn>
        <VxeColumn v-if="!isReadOnly" field="operate" title="操作" width="200">
          <template #default="{ row }">
            <ElButton link type="primary" @click="toAddJianCe(row)">
              新增监测点
            </ElButton>
            <ElButton link type="success" @click="toEdit(row)">
              编辑
            </ElButton>
            <ElButton link type="danger" @click="delEnt(row)">
              删除
            </ElButton>
          </template>
        </VxeColumn>
      </vxe-table>
    </div>
    <chanWuEdit ref="editRef" @submit="submitEdit" />
    <jianceEdit ref="jianceEditRef" @submit="submitJianCe" />
  </div>
</template>
